home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / cdsk_scp.cpp < prev    next >
C/C++ Source or Header  |  1999-06-10  |  5KB  |  280 lines

  1. //    This program is free software; you can redistribute it and/or modify
  2. //    it under the terms of the GNU General Public License as published by
  3. //    the Free Software Foundation; either version 2 of the License, or
  4. //    any later version.
  5. //
  6. //    This program is distributed in the hope that it will be useful,
  7. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. //    GNU General Public License for more details.
  10. //
  11. //    You should have received a copy of the GNU General Public License
  12. //    along with this program; if not, write to the Free Software
  13. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. //
  15.  
  16. #include "cdsk_scp.h"
  17. #include "autil.h"
  18. #include "cfile.h"
  19.  
  20. #define SCP_MAGIC 0xFDFD
  21.  
  22. CScp::CScp() : CDisk()
  23. {
  24.     #ifdef _MEMORY_DUMP_
  25.         printf( "CScp constructed: %08X\n", this );
  26.     #endif
  27. }
  28.  
  29. CScp::~CScp()
  30. {
  31.     #ifdef _MEMORY_DUMP_
  32.         printf( "CScp destructed: %08X\n", this );
  33.     #endif
  34. }
  35.  
  36. BOOL CScp::Load( char* szFname, BOOL, BOOL )
  37. {
  38.     WORD    wMagic;
  39.     BYTE    btSectorSize;
  40.     BYTE    btTracks;
  41.     BYTE    btSectorsPerTrack;
  42.  
  43.     CFile cf;
  44.  
  45.     if( !cf.Open( szFname ) )
  46.     {
  47.         sprintf( m_szLastError, "SCP: Can't open '%s'", szFname );
  48.         return FALSE;
  49.     }
  50.  
  51.     strcpy( m_szFname, szFname );
  52.  
  53.     wMagic = cf.readLEw();
  54.  
  55.     if ( wMagic != SCP_MAGIC )
  56.     {
  57.         sprintf( m_szLastError, "SCP: File '%s' is not an SCP file!", szFname );
  58.         return FALSE;
  59.     }
  60.  
  61.     btSectorSize = cf.readb();
  62.     btTracks = cf.readb();
  63.     btSectorsPerTrack = cf.readb();
  64.  
  65.     int iSectorSize;
  66.  
  67.     switch( btSectorSize )
  68.     {
  69.         case 0x80:
  70.             iSectorSize = 0x80;
  71.             break;
  72.  
  73.         case 0x00:
  74.             iSectorSize = 0x100;
  75.             break;
  76.  
  77.         default:
  78.         {
  79.             sprintf( m_szLastError, "SCP: Invalid sector size: %02X", btSectorSize );
  80.             return FALSE;
  81.         }
  82.     }
  83.  
  84.     DISK_GEOMETRY dg;
  85.     dg.iSides = 1;
  86.     dg.iTracks = btTracks;
  87.     dg.iSectorsPerTrack = btSectorsPerTrack;
  88.     dg.iBytesPerSector = iSectorSize;
  89.  
  90.     if ( !Format( &dg ) )
  91.         return FALSE;
  92.  
  93.     BYTE *pbtTable = new BYTE [ btSectorsPerTrack * btTracks ];
  94.  
  95.     if ( ! pbtTable )
  96.     {
  97.          sprintf( m_szLastError, "SCP: Not enough memory for sector table!" );
  98.          return FALSE;
  99.     }
  100.  
  101.     if ( !cf.Read( pbtTable, btSectorsPerTrack * btTracks ) )
  102.     {
  103.          sprintf( m_szLastError, "SCP: Can't read sector table!" );
  104.          return FALSE;
  105.     }
  106.  
  107.     BYTE abtBuff[ 0x100 ];
  108.     memset( abtBuff, 0, 0x100 );
  109.  
  110.     BYTE* pbtPtr = pbtTable;
  111.  
  112.     for( int iTrack = 0; iTrack < btTracks; iTrack++ )
  113.     {
  114.         for( int iSector = 0; iSector < btSectorsPerTrack; iSector++ )
  115.         {
  116.             if ( *pbtPtr )
  117.             {
  118.                 int iNowRead;
  119.  
  120.                 if ( !iTrack && ( iSector < 3 ) )
  121.                     iNowRead = 0x80;
  122.                 else
  123.                     iNowRead = iSectorSize;
  124.  
  125.                 int iReallyRead;
  126.  
  127.                 if ( !cf.Read( abtBuff, iNowRead, &iReallyRead ) || ( iNowRead != iReallyRead ) )
  128.                 {
  129.                     delete [] pbtTable;
  130.                      sprintf( m_szLastError, "SCP: Image broken!" );
  131.                      return FALSE;
  132.                 }
  133.  
  134.                 if ( !WriteSector( *pbtPtr + iTrack* btSectorsPerTrack, abtBuff ) )
  135.                 {
  136.                     delete [] pbtTable;
  137.                     return FALSE;
  138.                 }
  139.  
  140.             }
  141.             pbtPtr++;
  142.         }
  143.  
  144.     }
  145.  
  146.     delete [] pbtTable;
  147.  
  148.     cf.Close();
  149.     return TRUE;
  150.  
  151. }
  152.  
  153. #ifdef __CDISK_WRITE__
  154.  
  155. BOOL CScp::Save( char* szOutFile, BOOL bOverWrite )
  156. {
  157.     if ( !bOverWrite && !access( szOutFile, F_OK ) )
  158.     {
  159.         sprintf( m_szLastError, "SCP: File already exists! '%s'", szOutFile );
  160.         return FALSE;
  161.     }
  162.  
  163.     BYTE btSize;
  164.     BYTE btTracks;
  165.     BYTE btSpT;
  166.  
  167.     switch ( m_geometry.iBytesPerSector )
  168.     {
  169.         case 0x80:
  170.             btSize = 0x80;
  171.             break;
  172.  
  173.         case 0x100:
  174.         default:
  175.             btSize = 0x00;
  176.             break;
  177.     }
  178.  
  179.     BOOL bGood = FALSE;
  180.  
  181.     btTracks = m_geometry.iTracks;
  182.     btSpT = m_geometry.iSectorsPerTrack;
  183.  
  184.     if ( ( m_geometry.iTracks == 40 ) && ( m_geometry.iSectorsPerTrack == 18 ) )
  185.         bGood = TRUE;
  186.  
  187.     if ( ( m_geometry.iTracks == 40 ) && ( m_geometry.iSectorsPerTrack == 26 ) )
  188.         bGood = TRUE;
  189.  
  190.     if ( !bGood )
  191.     {
  192.         sprintf( m_szLastError, "SCP: Can't export, because of invalid disk size!" );
  193.         return FALSE;
  194.     }
  195.  
  196.     int iMapSize = m_geometry.iTracks * m_geometry.iSectorsPerTrack;
  197.  
  198.     BYTE* pMap = new BYTE [ iMapSize ];
  199.  
  200.     if ( !pMap )
  201.     {
  202.         sprintf( m_szLastError, "SCP: Can't allocate memory for map!" );
  203.         return FALSE;
  204.     }
  205.  
  206.     memset( pMap, 0, iMapSize );
  207.  
  208.  
  209.     CFile cf;
  210.  
  211.     if ( !cf.Create( szOutFile ) )
  212.     {
  213.         sprintf( m_szLastError, "SCP: Can't create '%s'", szOutFile );
  214.         delete [] pMap;
  215.         return FALSE;
  216.     }
  217.  
  218.     WORD wMagic = SCP_MAGIC;
  219.  
  220.     cf.writeLEw( wMagic );
  221.     cf.writeb( btSize );
  222.     cf.writeb( btTracks );
  223.     cf.writeb( btSpT );
  224.  
  225.     cf.Seek( iMapSize, SEEK_CUR );
  226.  
  227.     BYTE abtBuff[ 0x100 ];
  228.  
  229.     int iSectors = m_geometry.iSectors;
  230.  
  231.     for( int i = 0; i < iSectors; i++ )
  232.     {
  233.         if ( !ReadSector( abtBuff, i + 1 ) )
  234.         {
  235.             delete [] pMap;
  236.             cf.Close();
  237.             unlink( szOutFile );
  238.             return FALSE;
  239.         }
  240.  
  241.         int iBytesNow = ( i < 3 ) ? 0x80 : m_geometry.iBytesPerSector;
  242.  
  243.         if ( !IsBlockEmpty( abtBuff, iBytesNow ) )
  244.         {
  245.             int iWritten;
  246.             if ( !cf.Write( abtBuff, iBytesNow, &iWritten ) || ( iBytesNow != iWritten ) )
  247.             {
  248.                 sprintf( m_szLastError, "SCP: Error writing to '%s'", szOutFile );
  249.                 delete [] pMap;
  250.                 cf.Close( );
  251.                 unlink( szOutFile );
  252.                 return FALSE;
  253.             
  254.             }
  255.  
  256.             pMap[ i ] = ( i % btSpT ) + 1;
  257.         }
  258.     }
  259.  
  260.     cf.Seek( 5, SEEK_SET );
  261.  
  262.     if ( !cf.Write( pMap, iMapSize ) )
  263.     {
  264.         sprintf( m_szLastError, "SCP: Can't write!" );
  265.         delete [] pMap;
  266.         cf.Close( );
  267.         unlink( szOutFile );
  268.         return FALSE;
  269.     
  270.     }
  271.  
  272.     delete [] pMap;
  273.  
  274.     cf.Close();
  275.  
  276.     return TRUE;
  277. }
  278.  
  279. #endif //__CDISK_WRITE__
  280.